<?php
/**
 * Created by PhpStorm.
 * User: muhe0
 * Date: 2017/5/7
 * Time: 5:10
 */

namespace LDL\Twspider\C\Timer;

use SuperClosure\Serializer;
use VDB\Spider\Stackable as TimeBase;

class TimeJob  extends TimeBase
{

    //保存所有定时任务
    private  $job;
    //定时间隔
    private  $time ;
    public function  __construct($interval, \Closure $calld){
 	    $serializer = new Serializer();

	    $seri= $serializer->serialize($calld);

	     
	    $this->job=$seri ;

        $this->time=$interval ;
    }
    public function getJob(){
    
 	    $serializer = new Serializer();
	   return $serializer->unserialize($this->job);
    }
    
    public function cancel(){
        $this->can=true ;
    }
    private $can=false ;
    public function  start($arg,$_=null){
	$job=($this->getJob());
	while(!$this->can){
            call_user_func($job,$arg,$_);
            $this->synchronized(function (){
                $this->wait($this->time);
            });
     }
    }





}

